/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telas;

import connection.EMF;
import dao.DAOGenerico;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import model.Fornecedor;
import model.MateriaPrima;

/**
 *
 * @author Dentinho
 */
public class CadastrarMateriaPrima extends javax.swing.JDialog {

    private List<Fornecedor> listFornecedor = new ArrayList<>();
    private Fornecedor fornecedor;
    
    /**
     * Creates new form CadastrarMateriaPrima
     */
    public CadastrarMateriaPrima(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    CadastrarMateriaPrima() {
        initComponents();
        desativaCampos();
    }
    
    private void initAction(){
        fornecedor = new Fornecedor();
    }
    
    
    private void vazio(){
        
    }
    private void ativaCampos(){
        textDescricao.setEnabled(true);
        textPreco.setEnabled(true);
        comboFornecedor.setEnabled(true);
        comboUnidade.setEnabled(true);
        btNovo.setEnabled(false);
        btSalvar.setEnabled(true);
        btLimpar.setEnabled(true);
    }
    
    private void desativaCampos(){
        textDescricao.setEnabled(false);
        textPreco.setEnabled(false);
        comboFornecedor.setEnabled(false);
        comboUnidade.setEnabled(true);
        btNovo.setEnabled(true);
        btSalvar.setEnabled(false);
        btLimpar.setEnabled(true);
    }
    
    private void limpaCampos(){
        textDescricao.setText("");
        textPreco.setText("");
    }
    
    private void preencherFornecedor(){
        comboFornecedor.removeAllItems();
        initAction();
        DAOGenerico<Fornecedor> daof = new DAOGenerico<>(EMF.getEm(),Fornecedor.class);
        listFornecedor = daof.buscarTodos(Fornecedor.class);
        int x = 0;
        while(listFornecedor.size()>x){
            comboFornecedor.addItem(listFornecedor.get(x).getNomeFantasia());
            x++;
        }
    }
  

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        textDescricao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        textPreco = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        comboUnidade = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        comboFornecedor = new javax.swing.JComboBox();
        lbEro = new javax.swing.JLabel();
        btCancela = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btNovo = new javax.swing.JButton();
        btLimpar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro Materia Prima");

        painel.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Descrição");

        jLabel2.setText("Preço de Custo");

        jLabel3.setText("Unidade Medida");

        comboUnidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Quilo", "Duzia", "Pacote", "Unidade", "Metro" }));

        jLabel4.setText("Fornecedor");

        lbEro.setText("*");

        javax.swing.GroupLayout painelLayout = new javax.swing.GroupLayout(painel);
        painel.setLayout(painelLayout);
        painelLayout.setHorizontalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbEro)
                    .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(comboUnidade, 0, 242, Short.MAX_VALUE)
                        .addComponent(textDescricao)
                        .addComponent(textPreco)
                        .addComponent(comboFornecedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        painelLayout.setVerticalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textPreco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbEro)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        btCancela.setText("Cancelar e Sair");
        btCancela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelaActionPerformed(evt);
            }
        });

        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        btLimpar.setText("Limpar");
        btLimpar.setFocusable(false);
        btLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btLimpar.setName("btLimpar"); // NOI18N
        btLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLimparActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(painel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(btLimpar)
                        .addGap(15, 15, 15)
                        .addComponent(btNovo)
                        .addGap(18, 18, 18)
                        .addComponent(btCancela)
                        .addGap(0, 218, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btLimpar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btSalvar)
                        .addComponent(btNovo)
                        .addComponent(btCancela)))
                .addContainerGap(63, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelaActionPerformed
        dispose();
    }//GEN-LAST:event_btCancelaActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        vazio();
        int i = 0;
        if ("".equals(textDescricao.getText())) {
            i = 1;
            textDescricao.setBackground(Color.red);
        }
        if ("".equals(textPreco.getText())) {
            i = 1;
            textPreco.setBackground(Color.red);
        }
        
        if (i == 1) {
            lbEro.setText("Campos em destaque não podem ser nulos!");
            lbEro.setForeground(Color.red);
        } else {
            DAOGenerico<MateriaPrima> dao = new DAOGenerico<>(EMF.getEm(), MateriaPrima.class);
            MateriaPrima materia = new MateriaPrima();
            materia.setDescricao(textDescricao.getText());
            materia.setPrecoCusto(Float.parseFloat(textPreco.getText()));
            materia.setFornecedor(listFornecedor.get(comboFornecedor.getSelectedIndex()));
            materia.setUnidadeMedida(comboUnidade.getSelectedItem().toString());
            dao.salvar(materia);
            limpaCampos();
            desativaCampos();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        painel.setEnabled(true);
        preencherFornecedor();
        limpaCampos();
        ativaCampos();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLimparActionPerformed
        limpaCampos();
    }//GEN-LAST:event_btLimparActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastrarMateriaPrima.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastrarMateriaPrima.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastrarMateriaPrima.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastrarMateriaPrima.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CadastrarMateriaPrima dialog = new CadastrarMateriaPrima(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancela;
    private javax.swing.JButton btLimpar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox comboFornecedor;
    private javax.swing.JComboBox comboUnidade;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lbEro;
    private javax.swing.JPanel painel;
    private javax.swing.JTextField textDescricao;
    private javax.swing.JTextField textPreco;
    // End of variables declaration//GEN-END:variables
}
